package com.fs.system.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootConfiguration
public class CorsFilterConfig {

    @Bean
    public CorsFilter corsFilter(){
        //Cors的配置
        CorsConfiguration configuration = new CorsConfiguration();
        //允许的源
        configuration.addAllowedOrigin("*");
        //允许的请求头
        configuration.addAllowedHeader("*");
        //允许的请求方法
        configuration.addAllowedMethod("*");
        //预检的有效时间
        configuration.setMaxAge(1800L);
        //创建CorsConfigurationSource对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        //设置过滤器拦截的url, 配置
        configurationSource.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(configurationSource);
    }
}
